#coding=utf-8
from uliweb.orm import get_model
from uliweb.utils.generic import QueryView, RemoteField
from uliweb.utils.generic import ReferenceSelectField
from uliweb.form import DateField
from plugs.ui.jquery.jquery.widgets import DatePicker
from forms import QueryForm, QueryTaskForm, QueryRequirementForm, QueryAssignmentForm
from uliweb.form import *
from plugs.ui.jquery.uimultiselect.widgets import UIMultiSelectField
from plugs.ui.jquery.jqcombobox.widgets import JQComboBoxField

def get_record(model):
    def _f(id):
        from uliweb.orm import get_model
        
        if id:
            obj = get_model(model).get(int(id))
            if obj:
                return [(obj.id, unicode(obj))]
        return [('', '')]
    return _f

def get_links(title, links):
    from uliweb.core.template import template
    
    tmpl = """
<div class="hover-menu">
  <cite>{{=title}}<b></b></cite>
  <ul class="items">
    {{for url, name in links:}}
    <li><a href="{{=url}}">{{=name}}</a></li>
    {{pass}}
  </ul>
</div>
"""
    return template(tmpl, {'title':title, 'links':links})
    

def get_alt(model, name, condition=None):
    from uliweb import request
    
    alt = ''
    if request.values.get(name):
        M = get_model(model)
        if condition is None:
            condition = int(request.values.get(name))
        row = M.get(condition)
        if row:
            alt = unicode(row)
    return alt

def create_req_query(url):
    from uliweb import settings
    
    tx = ReferenceSelectField('tx', label='条线')
    kfzx = ReferenceSelectField('kfzx', label='开发中心')
    main_sys_kfzx = ReferenceSelectField('kfzx', label='牵头中心')
    title = UnicodeField('标题或编号')
    only_mine = SelectField('只显示我的', choices=settings.get_var('PARA/ONLY_MINE'), default='1')
    system = RemoteField('系统', url='/systems/query?user=', default=None, alt=get_alt('system', 'system'))
    care_level = SelectField('关注级别', choices=settings.get_var('PARA/CARE_LEVEL'))
    begin_date = DateField('开始日期', build=DatePicker)
    end_date = DateField('截止日期', build=DatePicker)
    closed = SelectField('显示已结束', choices=settings.get_var('PARA/YES_NO'), default='0')
    mode = UIMultiSelectField('实施方式', choices=settings.get_var('PARA/REQ_MODE'))
    
    selfdev_if = SelectField('是否参与系统自主率考核', choices=settings.get_var('PARA/YES_NO'))
    taskdev_if = SelectField('是否有关联开发任务', choices=settings.get_var('PARA/YES_NO'))
    stage_choices = []
    mode_names = dict(settings.get_var('PARA/REQ_MODE'))
    for k in sorted(settings.get_var('PARA/REQ_STAGE')):
        name = mode_names[k]
        s = [(name, x[0], x[1]) for x in settings.get_var('PARA/REQ_STAGE')[k]]
        stage_choices.extend(s)
    stage = UIMultiSelectField('所处阶段', choices=stage_choices)
    
    fields = [('tx', tx), ('kfzx', kfzx),  ('main_sys_kfzx',main_sys_kfzx), ('system', system), ('mode', mode), ('stage', stage),
        ('care_level', care_level), ('title', title), ('begin_date', begin_date), 
        ('end_date', end_date), ('closed', closed), ('selfdev_if',selfdev_if),
        ('only_mine', only_mine),('taskdev_if',taskdev_if)]
    layout = [('only_mine', 'tx', 'care_level', 'title', 'closed'), ('kfzx', 'system', 'selfdev_if'), 
        ('begin_date', 'end_date', 'main_sys_kfzx'), ('mode', 'stage','taskdev_if')]
    query = QueryView('requirement', ok_url=url, fields=fields, layout=layout, 
        form_cls=QueryForm)
    return query

def create_user_query(url):
    from uliweb.utils.generic import QueryView
    from uliweb.utils.generic import ReferenceSelectField
    
    tx = ReferenceSelectField('tx', label='条线')
    kfzx = ReferenceSelectField('kfzx', label='部门名称')
    fields = ('username', 'department', ('kfzx', kfzx), ('tx', tx)) 
    query = QueryView('user', ok_url=url, fields=fields)
    return query

def create_system_query(url):
    from uliweb.utils.generic import QueryView
    from uliweb.form import UnicodeField
    
    sys_name = UnicodeField('系统名称/ID/简称')
    fields = (('sys_name', sys_name), 'kfzx','sys_type')
    layout = [('sys_name', 'kfzx', 'sys_type')]
    query = QueryView('system', ok_url=url, fields=fields, layout=layout)
    return query

def create_task_query(task_type, url):
    from uliweb import request, settings
    from uliweb.orm import get_model
    
    def get_model_name(task_type):
        if task_type == 'develop':
            return 'tasks_develop'
        elif task_type == 'researches':
            return 'researches'
        else:
            return 'tasks'
    
    Requirement = get_model('requirement')
    Assignment = get_model('assignment')
    System = get_model('system')
    name = UnicodeField('名称或编号')
    memo = UnicodeField('备注')
    Task = get_model(get_model_name(task_type))
    status = UIMultiSelectField('任务执行情况', choices=settings.get_var('PARA/TASK_STATUS'))
    stage = UIMultiSelectField('阶段', choices=settings.get_var('PARA/TASK_STAGE'))
    only_mine = SelectField('只显示我的任务', choices=settings.get_var('PARA/ONLY_MINE'), default='1')
    closed = SelectField('显示已完成', choices=settings.get_var('PARA/YES_NO'), default='0')
    header_id = RemoteField('主任务', url='/tasks/search?header=', default=None, alt=get_alt('tasks_develop', 'header_id'))
    begin_date = DateField('投产开始日期', build=DatePicker)
    end_date = DateField('投产截止日期', build=DatePicker)
    stakeholder = RemoteField('任务责任人', url='/config/users/search', default=None, alt=get_alt('user', 'stakeholder'))
    task_sys = RemoteField('系统', url='/systems/query?user=', default=None, alt=get_alt('system', 'task_sys'))
    requirement = RemoteField('准开发需求', url='/requirements/search', default=None, alt=get_alt('requirement', 'requirement'))
    assignment = RemoteField('任务书', url='/assignments/query', default=None, alt=get_alt('assignment', 'assignment'))
    selfdev_if = SelectField('是否参与系统自主率考核', choices=settings.get_var('PARA/YES_NO'))
    begin_created_date = DateField('创建开始日期', build=DatePicker)
    end_created_date = DateField('创建截止日期', build=DatePicker)
    
    if task_type == 'develop':
        task_type = UIMultiSelectField('任务类型', choices=settings.get_var('PARA/TASK_TYPE_DEVELOP'))
        fields = [('requirement', requirement), 'kfzx', ('task_sys', task_sys), ('header_id', header_id),
            ('status', status), ('assignment', assignment), ('begin_date',begin_date), 
            ('end_date',end_date),('name', name),('task_type', task_type), ('stage', stage),
            ('closed', closed),('selfdev_if',selfdev_if), ('memo', memo), ('stakeholder', stakeholder),
            ('begin_created_date',begin_created_date),('end_created_date',end_created_date)]
#        if request.user.is_superuser:
        layout = [['name', 'closed'], ('kfzx', 'task_sys', 'stakeholder'), ['stage','task_type'], ['begin_date', 'end_date', 'selfdev_if'], ('requirement', 'assignment', 'header_id'), ('status', 'memo'),('begin_created_date','end_created_date')]
#        else:
#            layout = [['name', 'closed'], ('kfzx', 'task_sys', 'stakeholder'), ['stage','task_type'], ['begin_date', 'end_date', 'selfdev_if'], ('requirement', 'assignment', 'header_id'), ('status', 'memo')]
        if request.user.department in ('2', '3'):
            fields = [('only_mine', only_mine)] + fields
            layout[0] = ['only_mine'] + layout[0]
    elif task_type == 'researches':
        fields = [('requirement', requirement), 'kfzx', 'task_id', ('task_sys', task_sys), 
                    ('status', status), ('assignment', assignment),('name',name), ('closed', closed)]
        layout = [['name', 'closed'], ('kfzx', 'task_sys'), ('requirement', 'assignment', 'status') ]
        if request.user.department in ('2', '3'):
            fields = [('only_mine', only_mine)] + fields
            layout[0] = ['only_mine'] + layout[0]
    else:
        task_type = UIMultiSelectField('任务类型', choices=settings.get_var('PARA/TASK_TYPE_NONDEVELOP'))
        fields = [('requirement', requirement), 'kfzx', 'task_id', ('task_sys', task_sys), 
                    ('status', status), ('assignment', assignment),('name',name),('task_type', task_type)]
        layout = [['name','status'], ('kfzx', 'task_sys'), ('requirement', 'assignment'), 'task_type']
    
    query = QueryView(Task, ok_url=url, fields=fields, layout=layout, form_cls=QueryTaskForm)
    return query

def create_assignment_query(url):
    from uliweb import settings
    
    Requirement = get_model('requirement')
    ass_name = UnicodeField('名称或编号')
    System = get_model('system')
    Assignment = get_model('assignment')
    type = UIMultiSelectField('类型', choices=settings.get_var('PARA/ASSIGNMENT_TYPE'))
    stage_choices = []
    type_names = dict(settings.get_var('PARA/ASSIGNMENT_TYPE'))
    for k in sorted(settings.get_var('PARA/ASSIGNMENT_STAGE')):
        name = type_names[k]
        s = [(name, x[0], x[1]) for x in settings.get_var('PARA/ASSIGNMENT_STAGE')[k]]
        stage_choices.extend(s)
    stage = UIMultiSelectField('所处阶段', choices=stage_choices)
    status = UIMultiSelectField('执行状态', choices=settings.get_var('PARA/ASSIGNMENT_STATUS'))
    system = RemoteField('系统', url='/systems/query?user=', default=None, alt=get_alt('system', 'system'))
    requirement = RemoteField('准开发需求', url='/requirements/search', default=None, alt=get_alt('requirement', 'requirement'))
    fields = ['kfzx', ('status', status), ('requirement', requirement), ('system', system), 
                ('type', type), ('stage', stage), ('name', ass_name)]
    layout = [('name', 'kfzx'), ('type', 'stage'), ('status', 'requirement', 'system')]
    query = QueryView(Assignment, ok_url=url, fields=fields, layout=layout, 
        form_cls=QueryForm)
    return query

def create_workload_query(url):
    from uliweb import settings
    
    System = get_model('system')
    system = JQComboBoxField(label='系统', url='/systems/query?user=', get_record=get_record('system'), datatype=int)
    kfzx = ReferenceSelectField('kfzx', label='开发中心')
    begin_year_month = IntField('开始年月(yyyymm)')
    end_year_month = IntField('截止年月(yyyymm)')
    task_type = UIMultiSelectField('任务类型', choices=settings.get_var('PARA/TASK_TYPE'))
    fields = (('system', system), ('begin_year_month', begin_year_month), 
        ('end_year_month', end_year_month), ('task_type', task_type), ('kfzx', kfzx)) 
    layout = [('system', 'begin_year_month', 'end_year_month'), ('kfzx', 'task_type')]
    query = QueryView('tasks_details', ok_url=url, fields=fields, layout=layout, form_cls=QueryForm)
    return query
    
def create_audit_query(url):
    from uliweb import settings
    
    Audit = get_model('audit')
    Tables = get_model('tables')
    modify_flag = SelectField('修改标志', choices=settings.get_var('PARA/AUDIT_MODIFY_FLAG'))
    table_id = ReferenceSelectField('tables', label='表名', query=Tables.all().order_by(Tables.c.table_name))
    fields = (('table_id', table_id), 'obj_id', 'parent_id', 'title', ('modify_flag', modify_flag), 'modified_date', 'modified_user') 
    layout = [('table_id', 'obj_id', 'parent_id'), ('modify_flag', 'title'), ('modified_date', 'modified_user')]
    query = QueryView(Audit, ok_url=url, fields=fields, layout=layout, form_cls=QueryForm)
    return query

def create_test_query(url):
    from uliweb import request, settings
    
    System = get_model('system')
    query = None
    
    if request.user.is_superuser:
        query = None
    elif request.user.department == '2':
        if request.user.is_gm:
            query = query_user_system()
        else:
            query = request.user.users_systems.all()
    elif request.user.department == '3':
        if request.user.is_gm:
            query = None
        else:
            query = request.user.users_systems.all()
    
    only_mine = SelectField('只显示我的', choices=settings.get_var('PARA/ONLY_MINE'), default='1')
    kfzx = ReferenceSelectField('kfzx', label='开发中心')
    system = ReferenceSelectField('system', label='系统', query=query)
    function_type = SelectField('功能类型', choices=settings.get_var('PARA/FUNCTION_TYPE'))
#    change_status = SelectField('变动状态', choices=settings.get_var('PARA/CHANGE_STATUS'))
#    using_status = BooleanField('开放使用状态')
#    business_degree = SelectField('业务重要程度', choices=settings.get_var('PARA/BUSINESS_DEGREE'))
#    trading_frequency = BooleanField('是否属于访问频度高的交易')
    version_date_begin = DateField('投产:开始时间', build=DatePicker)
    version_date_end = DateField('投产:结束时间', build=DatePicker)
    
    Testplan_functions = get_model('testplan_functions')

    
    fields = (('system', system), ('function_type',function_type), 
#        ('change_status', change_status),
#        ('using_status', using_status), ('business_degree', business_degree), ('trading_frequency', trading_frequency),
        ('version_date_begin',version_date_begin),
        ('version_date_end',version_date_end),('kfzx',kfzx))
    layout = [('system'),('kfzx','function_type'),('version_date_begin','version_date_end')]
#    layout = [('system'),('function_type','change_status','using_status','business_degree'),('trading_frequency','version_date')]
    if request.user.department in ['2']:
        fields = (('only_mine', only_mine),('system', system), ('function_type',function_type), 
        ('version_date_begin',version_date_begin),
        ('version_date_end',version_date_end),('kfzx',kfzx))

        layout = [('only_mine','system'),('kfzx','function_type'),('version_date_begin','version_date_end')]
    
    query = QueryView(Testplan_functions, ok_url=url, fields=fields, layout=layout, form_cls=QueryForm)
    return query

def create_test_systemfunction_query(url):
    from uliweb import request, settings
    
    System = get_model('system')
    query = None
    
    if request.user.is_superuser:
        query = None
    elif request.user.department == '2':
        if request.user.is_gm:
            query = query_user_system()
        else:
            query = request.user.users_systems.all()
    elif request.user.department == '3':
        if request.user.is_gm:
            query = None
        else:
            query = request.user.users_systems.all()

    system = ReferenceSelectField('system', label='系统', query=query)

    function_type = SelectField('功能类型', choices=settings.get_var('PARA/FUNCTION_TYPE'))
    change_status = SelectField('变动状态', choices=settings.get_var('PARA/CHANGE_STATUS'))
    using_status = BooleanField('开放使用状态')
    business_degree = SelectField('业务重要程度', choices=settings.get_var('PARA/BUSINESS_DEGREE'))
    trading_frequency = BooleanField('是否属于访问频度高的交易')
    
    Test = get_model('test')
    fields = (('system', system), ('function_type',function_type), ('change_status', change_status),
        ('using_status', using_status), ('business_degree', business_degree), ('trading_frequency', trading_frequency),
        )
    layout = [('system'),('function_type','change_status','business_degree'),('trading_frequency','using_status')]

    query = QueryView(Test, ok_url=url, fields=fields, layout=layout, form_cls=QueryForm)
    return query

def create_manage_requirement_query(url):
    from uliweb import settings

    query = None
    
    tx = ReferenceSelectField('tx', label='条线')
    req_id = StringField(name='req_id', label='准开发需求编号')
    title = StringField(name='title', label='准开发需求名称')    
    kfzx = ReferenceSelectField('kfzx', label='相关开发中心')
    main_zx = ReferenceSelectField('kfzx', label='牵头开发中心')
    system = ReferenceSelectField('system', label='相关系统')
    main_sys = ReferenceSelectField('system', label='主系统')    
    begin_date = DateField('开始日期', build=DatePicker)
    end_date = DateField('截止日期', build=DatePicker)
    report_type = SelectField('报表类型', choices=settings.get_var('PARA/REPORT_TYPE'))
    
    fields = (('tx', tx), ('kfzx', kfzx), ('main_zx', main_zx), ('req_id',req_id),('title',title),('report_type',report_type),
        ('system', system),('main_sys', main_sys),('begin_date', begin_date), ('end_date', end_date),
        't_nproject','t_project','t_nassignment','t_undetermined','t_delay_flag','report_date')
        
    layout = [('report_type','tx','title'),('kfzx','main_zx'),('req_id'),
            ('begin_date', 'end_date'),'system','main_sys']
    query = QueryView('manage_requirement_week', ok_url=url, fields=fields, layout=layout, 
        form_cls=QueryRequirementForm)
    return query

def create_manage_assignments_query(url):
    from uliweb import settings, request
    
    def post_created_form(fcls, model):
        type = request.values.get('type', '')
        fcls.stage.choices = settings.PARA.ASSIGNMENT_STAGE.get(type, [])
    
    query = None
    status = SelectField('执行状态', choices=settings.get_var('PARA/ASSIGNMENT_STATUS'))
    system = ReferenceSelectField('system', label='系统')
    kfzx = ReferenceSelectField('kfzx', label='开发中心')
    tx = ReferenceSelectField('tx', label='开发中心')
    
    fields = (('tx',tx),'t_implement','t_js','t_ys','t_finish','t_delay',
        't_status', 't_task_delay', ('kfzx',kfzx), ('status', status), 
        ('system', system), 'type', 'stage', 'report_date', 'report_type')
    layout = [('kfzx', 'type', 'stage'), ('status', 'system')]
    query = QueryView('assignment_report_detail', ok_url=url, fields=fields, layout=layout, 
        form_cls=QueryAssignmentForm, post_created_form=post_created_form)
    return query
    
    
def create_manage_tasks_develop_query(url):
    from uliweb import settings
        
    requirement = ReferenceSelectField('requirement', label='准开发需求')
    status = SelectField('执行状态', choices=settings.get_var('PARA/TASK_STATUS'))
    begin_date = DateField('投产开始日期', build=DatePicker)
    end_date = DateField('投产截止日期', build=DatePicker)
    task_sys = ReferenceSelectField('system', label='系统')
    
    fields = ('t_implement','t_finish','t_version','t_delay','t_status','tx',
            ('requirement', requirement), 'kfzx', 'task_id', ('task_sys', task_sys), 
            ('status', status), 'name','task_type','report_date', 'report_type',
            'milestone_delay_num','milestone_change_num','milestone_total_num',
            ('begin_date',begin_date),('end_date',end_date))
    layout = [['kfzx', 'name','task_type'], ['task_id','task_sys']]
    query = QueryView('tasks_develop_report_detail', ok_url=url, fields=fields, layout=layout, 
        form_cls=QueryTaskForm)
    return query

def create_manage_work_total_query(url):
    from uliweb import settings

    query = None
    sys = get_model('system')
    system = ReferenceSelectField('system', label='系统', query=sys.filter(sys.c.sys_type!='5'))
    begin_date = DateField('开始日期', build=DatePicker)
    end_date = DateField('截止日期', build=DatePicker)
    type = SelectField('报表类型', default='1', choices=settings.get_var('PARA/REPORT_TYPE'))
    id_type = SelectField('报表维度', default='1', choices=settings.get_var('PARA/REPORT_DIMENSION'))

    fields = (('id_type',id_type),('type',type), ('system',system), 
        ('begin_date', begin_date), ('end_date', end_date))
    layout = [('id_type', 'type','begin_date', 'end_date'),'system']
    query = QueryView('manage_requirement_week', ok_url=url, fields=fields, layout=layout, 
        form_cls=QueryRequirementForm)
    return query

def create_manage_resource_zx_month_query(url):
    from uliweb.utils.generic import QueryView
    from uliweb.form import SelectField
    from datetime import date
    from forms import QueryRequirementForm
    from uliweb import settings, request
    
    query = None
    
    t_date = date.today()
    mon_def = t_date.month
    year_def = t_date.year
    if mon_def>1:
        mon_def = mon_def-1
    else:
        mon_def = 12
        year_def = year_def-1
    
    def_year = 0
    for key,value in settings.PARA.YEAR_LIST:
        if value == str(year_def):
            def_year = key 
            
    year = SelectField('请选择年', choices=settings.get_var('PARA/YEAR_LIST'), datatype=int)
    month = SelectField('请选择月', choices=settings.get_var('PARA/MONTH_LIST'), datatype=int)
    if 'year' in request.values:
        data = None
    else:
        data = {'year':def_year,'month':mon_def}

    fields = (('year',year),('month',month))
    layout = [('year','month')]
    query = QueryView('manage_requirement_week',ok_url=url, fields=fields, layout=layout, 
        form_cls=QueryRequirementForm, data=data)
    return query

def create_manage_resource_sys_month_query(url):
    from datetime import date
    from uliweb import settings, request
    
    query = None
    sys = get_model('system')
    t_date = date.today()
    mon_def = t_date.month
    year_def = t_date.year
    if mon_def>1:
        mon_def = mon_def-1
    else:
        mon_def = 12
        year_def = year_def-1
    
    def_year = 0
    for key,value in settings.PARA.YEAR_LIST:
        if value == str(year_def):
            def_year = key 
            
    year = SelectField('请选择年', choices=settings.get_var('PARA/YEAR_LIST'), datatype=int)
    month = SelectField('请选择月', choices=settings.get_var('PARA/MONTH_LIST'), datatype=int)
    system = RemoteField('系统', url='/systems/query?user=', default=None, alt=get_alt('system', 'system'))
    zx = ReferenceSelectField('kfzx', label='中心')
    if 'year' in request.values:
        data = None
    else:
        data = {'year':def_year,'month':mon_def}
    
    fields = (('year',year),('month',month),('system',system),('zx',zx))
    layout = [('year','month','zx','system'),]
    query = QueryView('manage_requirement_week',ok_url=url, fields=fields, layout=layout, 
        form_cls=QueryRequirementForm, data=data)
    return query

def create_manage_task_avgtime_query(url):
    query = None
    
    begin_date = DateField('开始日期', build=DatePicker)
    end_date = DateField('截止日期', build=DatePicker)
    fields = (('begin_date', begin_date), ('end_date', end_date))
    query = QueryView('tasks_develop', ok_url=url, fields=fields)
    return query

def create_manage_requirement_avgtime_query(url):
    from uliweb import settings
    
    query = None
    
    begin_date = DateField('开始日期', build=DatePicker)
    end_date = DateField('截止日期', build=DatePicker)
    mode = SelectField('实施方式', default='01', choices=settings.get_var('PARA/REQ_MODE'))

    fields = (('mode',mode), ('begin_date', begin_date), ('end_date', end_date))
    query = QueryView('requirementdetails', ok_url=url, fields=fields)
    return query

def create_develop_test_query(url):
    from uliweb import settings
    
    Task = get_model('tasks_develop')
    Requirement = get_model('requirement')
    Assignment = get_model('assignment')
    System = get_model('system')
    stage = UIMultiSelectField('阶段', choices=settings.get_var('PARA/TASK_STAGE'))
    header_id = StringField('主任务ID')
    begin_date = DateField('投产开始日期', build=DatePicker)
    end_date = DateField('投产截止日期', build=DatePicker)

    task_sys = JQComboBoxField(label='系统', url='/systems/query', get_record=get_record('system'), datatype=int)
    task_type = UIMultiSelectField('任务类型', choices=settings.get_var('PARA/TASK_TYPE_DEVELOP'))
    fields = ['kfzx', 'task_id', ('task_sys', task_sys), ('header_id', header_id),
             ('begin_date',begin_date), ('end_date',end_date),'name',('task_type', task_type), ('stage', stage)]
    layout = [['kfzx', 'name'], ['begin_date', 'end_date'], ('task_id', 'task_sys', 'stage')]

    
    query = QueryView(Task, ok_url=url, fields=fields, layout=layout, form_cls=QueryTaskForm)
    return query

def create_manage_resource_type_date_query(url):
    from uliweb.utils.generic import QueryView
    from uliweb.form import SelectField
    from datetime import date
    from forms import QueryRequirementForm
    from uliweb import settings, request
    
    query = None
    
    t_date = date.today()
    mon_def = t_date.month
    year_def = t_date.year
    if mon_def>1:
        mon_def = mon_def-1
    else:
        mon_def = 12
        year_def = year_def-1
    
    def_year = 0
    for key,value in settings.PARA.YEAR_LIST:
        if value == str(year_def):
            def_year = key 
            
    begin_year = SelectField('起始年月：',  choices=settings.get_var('PARA/YEAR_LIST'), datatype=int)
    begin_month = SelectField('-',  choices=settings.get_var('PARA/MONTH_LIST'), datatype=int)
    end_year = SelectField('终止年月：', choices=settings.get_var('PARA/YEAR_LIST'), datatype=int)
    end_month = SelectField('-', choices=settings.get_var('PARA/MONTH_LIST'), datatype=int)
    if 'begin_year' in request.values:
        data = None
    else:
        data = {'begin_year':def_year,'begin_month':1,'end_year':def_year,'end_month':mon_def}
    fields = (('begin_year',begin_year),('begin_month',begin_month),('end_year',end_year),('end_month',end_month))
    layout = [('begin_year','begin_month','end_year','end_month')]
    query = QueryView('manage_requirement_week',ok_url=url, fields=fields, layout=layout,data=data, 
        form_cls=QueryRequirementForm)
    return query

def create_weekreport_check_query(url):
    from uliweb import settings

    kfzx = ReferenceSelectField('kfzx', label='开发中心')
    title = UnicodeField('事项名称')
    care_level = SelectField('关注级别', choices=settings.get_var('PARA/CARE_LEVEL'))
    begin_date = DateField('周报日期', build=DatePicker)
    
    fields = (  ('title', title), ('begin_date', begin_date), ('kfzx', kfzx),
        ('care_level', care_level))
    layout = [('title', 'begin_date','care_level'),('kfzx')]
    query = QueryView('weekreport', ok_url=url, fields=fields, layout=layout, 
        form_cls=QueryForm)
    return query

def create_team_query(url):
    from uliweb.utils.generic import QueryView
    from uliweb.utils.generic import ReferenceSelectField
    

    kfzx = ReferenceSelectField('kfzx', label='开发中心')
    fields = ('name',  ('kfzx', kfzx)) 
    query = QueryView('team', ok_url=url, fields=fields)
    return query

def create_duties_query(url):
    fields = ('name',) 
    query = QueryView('duties', ok_url=url, fields=fields)
    return query

def create_position_query(url):
    from uliweb.utils.generic import QueryView
    from uliweb.utils.generic import ReferenceSelectField
    

    kfzx = ReferenceSelectField('kfzx', label='开发中心')
    fields = ('name',  ('kfzx', kfzx)) 
    query = QueryView('position', ok_url=url, fields=fields)
    return query

def create_Selfdev_if_details_query(url):
    begin_date = DateField('开始日期', build=DatePicker)
    end_date = DateField('结束日期', build=DatePicker)
    requirement_title = UnicodeField(label='准开发需求')
    task_title = UnicodeField(label='任务')
    
    fields = ( ('begin_date',begin_date), ('end_date',end_date),'system',  ('requirement_title',requirement_title), ('task_title',task_title)) 
    layout =[('begin_date','end_date'),('requirement_title', 'task_title'),'system']
    query = QueryView('selfdev_if_details', ok_url=url, fields=fields,
        layout=layout,form_cls=QueryForm)
    return query


def create_manage_riskpoint_executing_query(url):
    from uliweb import settings
    
    query = None
    id_type = SelectField('报表维度', default='1', choices=settings.get_var('PARA/RISKPOINT_REPORT_TYPE'))

    fields = [ ('id_type',id_type) ]
    layout = ['id_type']
    query = QueryView('riskpoint', ok_url=url, fields=fields, layout=layout, 
        form_cls=QueryRequirementForm)
    return query

def create_requirement_change_week_query(url):
    from uliweb import settings

    begin_date = DateField('周报日期', build=DatePicker)
    
    fields = [('begin_date', begin_date)]
    layout = ['begin_date']
    query = QueryView('weekreport', ok_url=url, fields=fields, layout=layout, 
        form_cls=QueryForm)
    return query
